<?php


namespace app\admin\controller;

use app\common\controller\CommonService;
use think\Controller;
use think\Db;
use think\facade\Validate;
use think\Request;

class HomePage extends Controller
{

    /**
     * 欢迎页数据
     * @return \think\response\Json
     */
    public function welcomePage()
    {
        for ($i = 1; $i <= 7; $i++) {
            $date = date('Y-m-d', strtotime('+' . $i - 7 . ' days', time()));
            $time = date('m-d', strtotime('+' . $i - 7 . ' days', time()));
            $data[$time] = $this->getData(strtotime($date), strtotime($date) + 86400);
        }
        foreach ($data as $k => $v){
            $data[$k]['orderSumSuperPartnerRebateFee'] = round($v['orderSumSuperPartnerRebateFee'],3);
            $data[$k]['orderSumUserBuyPrice'] = round($v['orderSumUserBuyPrice'],3);
        }
        return CommonService::success($data);
    }


    private function getData($startTime, $endTime)
    {
        $tbOrder = Db::name('tb_order')
            ->leftJoin('user_manage u','tb_order.uid = u.id')
            ->where('order_type', 1)
            ->where('order_status', '<>', 0)
            ->where('is_weiquan', 0)
            ->whereBetween('order_time', [date("Y-m-d H:i:s", $startTime), date("Y-m-d H:i:s", $endTime)])
            ->field("count(*) as orderCount,sum(user_buy_price) as orderSumUserBuyPrice,sum(super_partner_rebate_fee) as orderSumSuperPartnerRebateFee")
            ->find();
        $jdOrder = Db::name('jd_order')
            ->leftJoin('user_manage u','jd_order.uid = u.id')
            ->where('order_status', '<>', 0)
            ->whereBetween('order_time', [$startTime * 1000, $endTime * 1000])
            ->field("count(*) as orderCount,sum(user_buy_price) as orderSumUserBuyPrice,sum(super_partner_rebate_fee) as orderSumSuperPartnerRebateFee")
            ->find();
        $pddOrder = Db::name('pdd_order')
            ->leftJoin('user_manage u','pdd_order.uid = u.id')
            ->where('order_status', '<>', 0)
            ->whereBetween('order_time', [$startTime, $endTime])
            ->field("count(*) as orderCount,sum(user_buy_price) as orderSumUserBuyPrice,sum(super_partner_rebate_fee) as orderSumSuperPartnerRebateFee")
            ->find();
        $data['addUser'] = Db::name('user_manage')->whereBetween('time', [date('Y-m-d H:i:s', $startTime), date('Y-m-d H:i:s', $endTime)])->count();
        $data['orderCount'] = $tbOrder['orderCount'] + $jdOrder['orderCount'] + $pddOrder['orderCount'];
        $data['orderSumUserBuyPrice'] = $tbOrder['orderSumUserBuyPrice'] + $jdOrder['orderSumUserBuyPrice'] + $pddOrder['orderSumUserBuyPrice'];
        $data['orderSumSuperPartnerRebateFee'] = $tbOrder['orderSumSuperPartnerRebateFee'] + $jdOrder['orderSumSuperPartnerRebateFee'] + $pddOrder['orderSumSuperPartnerRebateFee'];
        return $data;
    }

}